package com.prs.crm.action.hr;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.hr.JobTitle;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeCommon;
import com.prs.crm.service.hr.JobTitleService;
import com.prs.crm.service.sys.CodeCommonService;
import com.prs.crm.service.sys.CodeService;

public class JobTitleAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private JobTitle jobTitle;
	private JobTitleService jobTitleService;
	private PaginationSupport page;
	private Integer[] jobTitleIds;

	private CodeCommon codeCommon;
	private CodeCommonService codeCommonService;
	private String Encode;
	private Code code;
	private CodeService codeService;

	private Collection<CodeCommon> codelist;

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	public CodeCommon getCodeCommon() {
		return codeCommon;
	}

	public void setCodeCommon(CodeCommon codeCommon) {
		this.codeCommon = codeCommon;
	}

	public CodeCommonService getCodeCommonService() {
		return codeCommonService;
	}

	public void setCodeCommonService(CodeCommonService codeCommonService) {
		this.codeCommonService = codeCommonService;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Collection<CodeCommon> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeCommon> codelist) {
		this.codelist = codelist;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public Integer[] getJobTitleIds() {
		return jobTitleIds;
	}

	public void setJobTitleIds(Integer[] jobTitleIds) {
		this.jobTitleIds = jobTitleIds;
	}

	@Action("viewJobTitle")
	public String view() {
		return SUCCESS;
	}

	@Action("listJobTitle")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				JobTitle.class);
		this.setPage(this.getJobTitleService().listByPage(criteria,
				getPaginationHelper().prepare(page)));
		return SUCCESS;
	}

	@Action("selectJobTitle")
	public String selectJobTitle() {
		this.execute();
		return SUCCESS;
	}

	@Action("editJobTitle")
	public String edit() {
		this.codelist = this.getCodeCommonService().getCodeCommonByCodeType(
				"职称");
		return SUCCESS;
	}

	@Action(value = "saveJobTitle", results = {
			@Result(name = "success", type = "redirect", location = "listJobTitle"),
			@Result(name = "error", type = "chain", location = "editJobTitle") })
	public String save() {
		if (jobTitle.getName() == null || "".equals(jobTitle.getName().trim())) {
			addActionError("职称不能为空");
			return ERROR;
		}
		if (jobTitle.getId() != null) {
			this.getJobTitleService().save(getJobTitle());
		} else {
			if (jobTitle.getJobTitlecode() == null
					|| "".equals(jobTitle.getJobTitlecode().trim())) {
				addActionError("编码不能为空");
				return ERROR;
			}
			if ("n".equals(Encode)) {
				Code codes = new Code();
				codes.setCodeValue(jobTitle.getJobTitlecode());
				codes.setEntityName(this.getClass().getName());
				codes.setCodeType("手动编码");
				codes.setCodeMolde("职称");
				this.getCodeService().save(codes);
				jobTitle.setCode(codes);
				this.getJobTitleService().save(getJobTitle());
			} else {
				CodeCommon Vob = this.getCodeCommonService().getVobject(
						codeCommon.getId());
				Code codes = new Code();
				codes.setEntityName(this.getClass().getName());
				codes.setCodeValue(jobTitle.getJobTitlecode());
				Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
				this.codeCommonService.saveOrUpdate(Vob);
				codes.setCodeType(Vob.getCodePlan());
				codes.setCodeMolde("职称");
				this.getCodeService().save(codes);
				jobTitle.setCode(codes);
				this.getJobTitleService().save(getJobTitle());
			}
		}
		return SUCCESS;
	}

	@Action(value = "removeJobTitle", results = {
			@Result(name = "success", type = "redirect", location = "listJobTitle"),
			@Result(name = "error", type = "chain", location = "listJobTitle") })
	public String remove() {
		if (jobTitleIds == null) {
			this.addActionError("请选择要删除的职称");
			return ERROR;
		}
		for (Integer id : jobTitleIds) {
			JobTitle title = this.getJobTitleService().get(id);
			if (title.getResumes().size() != 0) {
				this.addActionError("已有人员引用，不能进行删除");
				return ERROR;
			}
		}
		this.getJobTitleService().batchRemove(jobTitleIds);
		return SUCCESS;
	}

	public JobTitle getJobTitle() {
		return jobTitle;
	}

	public void setJobTitle(JobTitle jobTitle) {
		this.jobTitle = jobTitle;
	}

	public PaginationSupport getPage() {
		return page;
	}

	public void setPage(PaginationSupport page) {
		this.page = page;
	}

	public void setJobTitleService(JobTitleService jobTitleService) {
		this.jobTitleService = jobTitleService;
	}

	public JobTitleService getJobTitleService() {
		return jobTitleService;
	}

	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}

}
